package com.irdstudio.efp.flow.service;

import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.riskm.service.facade.ColltDelayCaseAppService;
import com.irdstudio.efp.riskm.service.facade.ColltDelayCaseRecordService;
import com.irdstudio.efp.riskm.service.facade.ColltTaskFlowAppService;
import com.irdstudio.efp.riskm.service.vo.ColltDelayCaseAppVO;
import com.irdstudio.efp.riskm.service.vo.ColltDelayCaseRecordVO;
import com.irdstudio.efp.riskm.service.vo.ColltTaskFlowAppVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 需求编号【2019D0509】
 * 催收人工流转服务类
 * @author qiuyf
 * @version 0.1 初始为0.1,后续变更则修改
 * @since 2019/9/30 10:54
 */
@Service("colltTaskFlowAppCommService")
public class ColltTaskFlowAppCommService {

    @Autowired
    @Qualifier("colltDelayCaseRecordService")
    private ColltDelayCaseRecordService colltDelayCaseRecordService;

    @Autowired
    @Qualifier("colltDelayCaseAppService")
    private ColltDelayCaseAppService colltDelayCaseAppService;

    @Autowired
    @Qualifier("colltTaskFlowAppService")
    private ColltTaskFlowAppService colltTaskFlowAppService;

    public int countDelayIng(String colltTaskNo){
        int result = 0;
        ColltDelayCaseRecordVO colltDelayCaseRecordVO = new ColltDelayCaseRecordVO();
        colltDelayCaseRecordVO.setColltTaskNo(colltTaskNo);
        List<ColltDelayCaseRecordVO> cdcrVoList = colltDelayCaseRecordService.queryByDelayCaseRecords(colltDelayCaseRecordVO);
        if(Objects.nonNull(cdcrVoList) && !cdcrVoList.isEmpty()){
            List<String> delayCaseAppList = cdcrVoList.stream().map(ColltDelayCaseRecordVO::getDelayCaseAppSerno).collect(Collectors.toList());
            List<ColltDelayCaseAppVO> cdcaVoList = colltDelayCaseAppService.queryByDelayCaseApp(delayCaseAppList);
            if(Objects.nonNull(cdcaVoList) && !cdcaVoList.isEmpty()){
                List<ColltDelayCaseAppVO> colltDelayCaseAppVOS = cdcaVoList.stream().filter(cdcaVo -> {
                    return ConsoleConstant.APRV_STATUS_02.equals(cdcaVo.getApproveStatus());
                }).collect(Collectors.toList());
                if(Objects.nonNull(colltDelayCaseAppVOS)){
                    result = colltDelayCaseAppVOS.size();
                }
            }
        }
        return result;
    }

    public int countFlowIng(String colltTaskNo){
        int result = 0;
        ColltTaskFlowAppVO colltTaskFlowAppVO = new ColltTaskFlowAppVO();
        colltTaskFlowAppVO.setColltTaskNo(colltTaskNo);
        List<ColltTaskFlowAppVO> ctfaVoList = colltTaskFlowAppService.queryByColltTaskNo(colltTaskFlowAppVO);
        if(Objects.nonNull(ctfaVoList) && !ctfaVoList.isEmpty()){
            List<ColltTaskFlowAppVO> colltTaskFlowAppVOS = ctfaVoList.stream().filter(ctfaVO -> {
                return ConsoleConstant.APRV_STATUS_02.equals(ctfaVO.getApproveStatus());
            }).collect(Collectors.toList());
            if(Objects.nonNull(colltTaskFlowAppVOS)){
                result = colltTaskFlowAppVOS.size();
            }
        }
        return result;
    }

}
